﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class FuLiao_CaiGou_JiHua_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShengChanDanWei_DataBind();               
                GridView_Bounding();
            }
        }
        private void ShengChanDanWei_DataBind()
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            String sql = "SELECT [ID], [名称] FROM [生产单位]";
            SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.ShengChanDanWei.DataSource = ds;
            this.ShengChanDanWei.DataBind();
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string jihua=this.JiHua.Text;
            if (jihua == "")
            {
                this.Message.Text = ".请选择计划";
                return;
            }
            else
            {
              GridView_Bounding();
            }
        }
        public void GridView_Bounding()
        {
            string sql = @"select 辅料采购明细.ID,辅料采购计划.计划名称,品名,规格,单位,计划采购量, 日期 from 辅料采购明细 inner join 辅料采购计划 on 辅料采购计划.ID=辅料采购明细.辅料采购计划ID inner join 辅料 on 辅料.ID=辅料采购明细.辅料ID where 辅料采购明细.辅料采购计划ID like '" + this.JiHua.Text + "'";
            this.SqlDataSource1.SelectCommand = sql;
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShengChanDanWei_DataBind();
            this.GridView1_DataBound(null, null);
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void JiHua_DataBound(object sender, EventArgs e)
        {
            GridView_Bounding();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        public void ToWord(System.Web.UI.Control ctl, String filetype, String filename)
        {

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(filename) + ".doc");

            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //更改ContentType的值为ms-word即可实现导出到Word  
            HttpContext.Current.Response.ContentType = filetype;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }

        protected void ToWord_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                this.GridView1.Rows[i].Cells[7].FindControl("CaiGouXuQiuLiang").Visible = false;
                this.GridView1.Rows[i].Cells[7].FindControl("LblCaiGouXuQiuLiang").Visible = true;
                ((Label)this.GridView1.Rows[i].Cells[7].FindControl("LblCaiGouXuQiuLiang")).Text = ((TextBox)this.GridView1.Rows[i].Cells[7].FindControl("CaiGouXuQiuLiang")).Text;
            }
            this.GridView1.Columns[8].Visible = false;
            this.GridView1.ShowHeader=true;
            this.GridView1.Caption ="<h2>"+this.ShengChanDanWei.Text+ "辅料采购计划"+"</h2></br>"+System.DateTime.Now;            
            ToWord(this.GridView1, "application/ms-word ", "jihua");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string i = e.Row.Cells[2].Text;
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                String strsql1 = "select sum(库存) from 辅料 where 品名='" + e.Row.Cells[2].Text+"'";
                SqlDataAdapter sda1 = new SqlDataAdapter(strsql1, Conn);
                DataTable dt1 = new DataTable();
                Conn.Open();
                sda1.Fill(dt1);
                e.Row.Cells[6].Text = dt1.Rows[0][0].ToString();
                Conn.Close();
                Conn.Dispose();
            }
        }
       
       
    }
}
